#include "gtest/gtest.h"
#include "../BaseUtil/MyTest.h"
#include "DOMEventImp.h"
namespace DOMEventImp {

class EventImpTest : public testing::Test {
protected:
	class DummyEventTarget : IMPLEMENTS( EventTarget ) {
	public:
		virtual void addEventListener(DOMString type, EventListener * listener, bool useCapture) {}
		virtual void removeEventListener(DOMString type, EventListener * listener, bool useCapture) {}
		virtual bool dispatchEvent(Event * evt) throw(EventExceptionPtr) { return false; }
	};

	virtual void SetUp() {
		evtTarget = new DummyEventTarget();
		evtCurrentTarget = new DummyEventTarget();
	}
    virtual void TearDown() {
    	delete evtTarget;
    	delete evtCurrentTarget;
    }

	EventTarget * evtTarget;
	EventTarget * evtCurrentTarget;

};

TEST_F(EventImpTest, Observers) {
	EventPtr evt = new EventImp(evtTarget, evtCurrentTarget, "userDefined", false, false);
	ASSERT_EQ(evtTarget, evt->getTarget());
	ASSERT_EQ(evtCurrentTarget, evt->getCurrentTarget());
	ASSERT_STREQ("userDefined", evt->getType().c_str());
	ASSERT_EQ(false, evt->isBubbles());
	ASSERT_EQ(false, evt->isCancelable());
	ASSERT_TRUE(evt->getTimeStamp() > time(0) - 10000);
	ASSERT_EQ(Event::CAPTURING_PHASE, evt->getEventPhase());
}


TEST_F(EventImpTest, ObserversBubble) {
	EventPtr evtBubble = new EventImp(NULL, NULL, "", true, false);
	ASSERT_TRUE(evtBubble->isBubbles());
}

TEST_F(EventImpTest, ObserversCancel) {
	EventPtr evtCancel = new EventImp(NULL, NULL, "", false, true);
	ASSERT_TRUE(evtCancel->isCancelable());
}

TEST_F(EventImpTest, EventPhase) {
	EventImpPtr evt = new EventImp(NULL, NULL, "userDefined", true, true);

	ASSERT_EQ(Event::CAPTURING_PHASE, evt->getEventPhase());

	evt->setEventPhase(Event::AT_TARGET);
	ASSERT_EQ(Event::AT_TARGET, evt->getEventPhase());
	ASSERT_EQ(Event::AT_TARGET, evt->getEventPhase());

	evt->setEventPhase(Event::BUBBLING_PHASE);
	ASSERT_EQ(Event::BUBBLING_PHASE, evt->getEventPhase());
	ASSERT_EQ(Event::BUBBLING_PHASE, evt->getEventPhase());
}

TEST_F(EventImpTest, StopPropagation) {
	EventImpPtr evt = new EventImp(NULL, NULL, "userDefined", true, true);

	evt->stopPropagation();
	ASSERT_TRUE( evt->isStopPropagation() );
}

TEST_F(EventImpTest, NonCancelableStopPropagation) {
	EventImpPtr evt = new EventImp(NULL, NULL, "userDefined", true, false);

	evt->stopPropagation();
	ASSERT_FALSE( evt->isStopPropagation() );
}

TEST_F(EventImpTest, PreventDefault) {
	EventImpPtr evt = new EventImp(NULL, NULL, "userDefined", true, true);

	evt->preventDefault();
	ASSERT_TRUE( evt->isPreventDefault() );
}

TEST_F(EventImpTest, NonCancelablePreventDefault) {
	EventImpPtr evt = new EventImp(NULL, NULL, "userDefined", true, false);

	evt->preventDefault();
	ASSERT_FALSE( evt->isPreventDefault() );
}

}
